How to Add and Use Remote Feature Flags in a Plugin
Overview
Feature flags allow you to enable or disable specific functionality dynamically, making it easier to roll out new features. From now on, feature flags will not be provided in the common scope. Instead, feature flags should be defined within the plugin itself. This document outlines the steps developers need to follow to add and use remote feature flags in a plugin.
Steps to Follow
1. Define the Feature Flag Name
- The feature flag name should be common for both platforms (iOS and Android).
- If the feature flag is specific to one platform, specify this in the CMS configuration.
- Define the minimum application version and other CMS-required details in the CMS configuration or communicate with the CMS team.
- For more details, refer to the relevant document [link].
2. Add the Feature Flag Key in the Plugin
- On the application side, if you are adding a flag for a particular plugin:
- Create an extension of the Constants file in that plugin.
- Inside the extension, create a
FeatureFlags
enum and define your key (use the same key shared with the CMS). - If a
FeatureFlags
enum already exists in the Constants file, add the new key to it.
Example:
extension ChallengeConstants {
enum FeatureFlags {
static let challengeQuizValidator = "ChallengeQuizValidator"
}
}
3. Retrieve the Feature Flag Value in the ViewModel
- If you need to check the feature flag logic, retrieve the feature flag value in the ViewModel.
- Use the
featureFlagManager
to retrieve the value of the feature flag.
Example:
let isEnabled = featureFlagManager.getValue(for: HomeConstants.FeatureFlags.personalizedContent)
- If additional logic is required, ensure it is implemented in the ViewModel to keep the code modular and maintainable.
4. Use the Feature Flag in Your Plugin
- Once the feature flag value is fetched, use it to toggle functionality in your plugin.
- Ensure that the feature flag logic is implemented in a way that does not affect the performance of the application.
Example:
if isEnabled {
// Enable the feature
} else {
// Disable the feature
}
Best Practices
- Use Descriptive Names: Ensure feature flag names are meaningful and easy to understand.
- Keep Flags Short-Lived: Remove unused feature flags to avoid clutter.
- Test Thoroughly: Test feature flags in both enabled and disabled states.
- Coordinate with CMS Team: Ensure proper configuration and documentation in the CMS.
Example Workflow
-
Define the Feature Flag in CMS:
- Add the feature flag name, description, and minimum application version in the CMS.
- Share the feature flag key with the development team.
-
Add the Feature Flag Key in the Plugin:
- Create or update the
FeatureFlags
enum in the plugin's Constants file.
- Create or update the
-
Retrieve the Feature Flag Value:
- Use the
featureFlagManager
to retrieve the value of the feature flag in the ViewModel.
- Use the
-
Implement the Feature Flag Logic:
- Use the fetched value to toggle functionality in the plugin.
Conclusion
By following these steps, you can effectively add and manage remote feature flags within your plugin. This approach ensures better modularity, maintainability, and flexibility in your application, allowing you to roll out features safely and dynamically.